/*
 * Copyright (C), 2014-2016, org.esaydev
 * FileName: HandleMenu.java
 * Author:   jian
 * Date:     2016年3月22日 
 */
package org.easydev.wechat.client.handle;

import org.apache.commons.lang3.StringUtils;
import org.easydev.wechat.client.dto.base.BaseParam;
import org.easydev.wechat.client.util.WechatReqConfig;
import org.easydev.wechat.common.constants.WechatErrorCode;
import org.easydev.wechat.common.exception.WechatException;
import org.easydev.wechat.common.util.HttpUtil;

import com.alibaba.fastjson.JSONObject;

/**
 * <Description>菜单操作的处理 <br>
 * 
 * @author jian<br>
 */
public class HandleMenu extends WechatRequestHandle {

    /**
     * 处理菜单操作<br>
     * {@inheritDoc}
     */
    protected JSONObject dohandle(BaseParam parsms, WechatReqConfig config) throws WechatException {
        if (null == parsms || null == config) {
            throw new WechatException(WechatErrorCode.PARAMS_ERROR);
        }
        if ("GET".equalsIgnoreCase(config.getMethod())) {
            String wechatRes = HttpUtil.sendGet(config.getUrl().concat(castJsonToQueryString((JSONObject) JSONObject.toJSON(parsms))));
            if (StringUtils.isBlank(wechatRes)) {
                throw new WechatException(WechatErrorCode.WECHAT_RESULT_ERROR);
            }
            return JSONObject.parseObject(wechatRes);
        } else if ("POST".equalsIgnoreCase(config.getMethod())) {
            String wechatRes = HttpUtil.sendPost(config.getUrl(), JSONObject.toJSONString(parsms));
            if (StringUtils.isBlank(wechatRes)) {
                throw new WechatException(WechatErrorCode.WECHAT_RESULT_ERROR);
            }
            return JSONObject.parseObject(wechatRes);
        } else {
            throw new WechatException(WechatErrorCode.PARAMS_ERROR);
        }

    }

}
